package bean;

import android.content.SharedPreferences;

import utils.Constant;
import utils.SharedPreferenceUtil;


/**
 * 当前登录用户
 *
 * @author: xiaoluo
 * @date: 2017-01-20 10:43
 */
public class LoginUser {
    private int sex;
    private int status;
    private String nickname;
    private String avatar;
    private int liked_count;
    private String city;
    private int id;
    private String username;
    private String bg_image;
    private String email;
    private int follower_count;
    private String province;
    private int followed_count;
    private String signature;
    private String mobile;
    private int ask_count;
    private int reply_count;
    private int total_reply_count;
    private int unread_message_count;
    private String imToken;

    private static LoginUser instance = null;

    public static synchronized LoginUser getInstance() {
        if (instance == null) {
            instance = getLocalUserData();
        }
        return instance;
    }

    /**
     *  从sp中读取数据,返回LoginUser实例
     */
    private static LoginUser getLocalUserData() {
        LoginUser user = new LoginUser();
        SharedPreferences sp = SharedPreferenceUtil.getSp();

        user.id = sp.getInt(Constant.SPKey.UID, -1);
        user.nickname = sp.getString(Constant.SPKey.NICK_NAME, "");
        user.username = sp.getString(Constant.SPKey.USER_NAME, "");
        user.signature = sp.getString(Constant.SPKey.SIGNATURE, "");
        user.avatar = sp.getString(Constant.SPKey.AVATAR_URL, "");
        user.sex = sp.getInt(Constant.SPKey.SEX, 0);
        user.bg_image = sp.getString(Constant.SPKey.BG_IMAGE, "");
        user.email = sp.getString(Constant.SPKey.EMAIL, "");
        user.followed_count = sp.getInt(Constant.SPKey.FELLOW_COUNT, 0);
        user.follower_count = sp.getInt(Constant.SPKey.FAN_COUNT, 0);
        user.liked_count = sp.getInt(Constant.SPKey.LIKE_COUNT, 0);
        user.city = sp.getString(Constant.SPKey.CITY, "");
        user.province = sp.getString(Constant.SPKey.PROVINCE, "");
        user.mobile = sp.getString(Constant.SPKey.PHONE, "");
        user.ask_count = sp.getInt(Constant.SPKey.ASK_COUNT, 0);
        user.reply_count = sp.getInt(Constant.SPKey.REPLY_COUNT, 0);
        user.unread_message_count = sp.getInt(Constant.SPKey.UNREAD_COUNT, 0);
        user.total_reply_count = sp.getInt(Constant.SPKey.TOTAL_REPLY_COUNT, 0);

        return user;
    }

    /**
     *  从接口返回数据获取登录用户数据
     */
    public void initFromResponse(TupaiUser data) {
        id = data.getId();
        nickname = data.getNickname();
        signature = data.getSignature();
        avatar = data.getAvatar();
        sex = data.getSex();
        bg_image = data.getBg_image();
        city = data.getCity();
        province = data.getProvince();
        mobile = data.getMobile();
        username = data.getUsername();
        email = data.getEmail();
        liked_count = data.getLiked_count();
        follower_count = data.getFollower_count();
        followed_count = data.getFollowed_count();
        ask_count = data.getAccount().getAsk_count();
        reply_count = data.getAccount().getReply_count();
        unread_message_count = data.getUnread_message_count();
        total_reply_count = data.getAccount().getTotal_reply_count();

        savaDataSP();
    }

    /**
     *  保存用户数据到SP
     */
    private void savaDataSP() {
        SharedPreferences.Editor editor = SharedPreferenceUtil.getEditor();

        editor.putInt(Constant.SPKey.UID, id);
        editor.putString(Constant.SPKey.NICK_NAME, nickname);
        editor.putString(Constant.SPKey.SIGNATURE, signature);
        editor.putString(Constant.SPKey.AVATAR_URL, avatar);
        editor.putInt(Constant.SPKey.SEX, sex);
        editor.putString(Constant.SPKey.BG_IMAGE, bg_image);
        editor.putString(Constant.SPKey.CITY, city);
        editor.putString(Constant.SPKey.PROVINCE, province);
        editor.putString(Constant.SPKey.PHONE, mobile);
        editor.putString(Constant.SPKey.USER_NAME, username);
        editor.putString(Constant.SPKey.EMAIL, email);
        editor.putInt(Constant.SPKey.LIKE_COUNT, liked_count);
        editor.putInt(Constant.SPKey.FOCUS_COUNT, followed_count);
        editor.putInt(Constant.SPKey.FAN_COUNT, follower_count);
        editor.putInt(Constant.SPKey.ASK_COUNT, ask_count);
        editor.putInt(Constant.SPKey.REPLY_COUNT, reply_count);
        editor.putInt(Constant.SPKey.TOTAL_REPLY_COUNT, total_reply_count);
        editor.putInt(Constant.SPKey.UNREAD_COUNT, unread_message_count);

        editor.apply();
    }

    /**
     * 存取token相关
     */
    public static final class TokenVerity {
        /**
         * 设置token
         */
        public static void setToken(String token) {
            SharedPreferences.Editor editor = SharedPreferenceUtil.getEditor();
            editor.putString(Constant.SPKey.TOKEN, token);
            editor.commit();
        }

        /**
         * 获取token
         */
        public static String getToken() {
            String token = SharedPreferenceUtil.getSp().getString(Constant.SPKey.TOKEN, "");
            return token;
        }
    }

    public String getImToken() {
        return imToken;
    }

    public void setImToken(String imToken) {
        this.imToken = imToken;
    }

    public int getTotal_reply_count() {
        return total_reply_count;
    }

    public void setTotal_reply_count(int total_reply_count) {
        this.total_reply_count = total_reply_count;
    }

    public int getUnread_message_count() {
        return unread_message_count;
    }

    public void setUnread_message_count(int unread_message_count) {
        this.unread_message_count = unread_message_count;
    }

    public int getAsk_count() {
        return ask_count;
    }

    public void setAsk_count(int ask_count) {
        this.ask_count = ask_count;
    }

    public int getReply_count() {
        return reply_count;
    }

    public void setReply_count(int reply_count) {
        this.reply_count = reply_count;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public int getLiked_count() {
        return liked_count;
    }

    public void setLiked_count(int liked_count) {
        this.liked_count = liked_count;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getBg_image() {
        return bg_image;
    }

    public void setBg_image(String bg_image) {
        this.bg_image = bg_image;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getFollower_count() {
        return follower_count;
    }

    public void setFollower_count(int follower_count) {
        this.follower_count = follower_count;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public int getFollowed_count() {
        return followed_count;
    }

    public void setFollowed_count(int followed_count) {
        this.followed_count = followed_count;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}
